package com.yuuzy.view;

import com.alibaba.druid.util.StringUtils;
import com.yuuzy.dao.UserDAO;
import com.yuuzy.model.User;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//系统登录界面
public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField userNameText;
    private JPasswordField passwordText;

    private UserDAO userDAO = new UserDAO();

    /**
     * Create the frame.
     */
    public Login() {

        setTitle("管理员登录");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 982, 781);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("  金融管理系统");
        lblNewLabel.setFont(new Font("宋体", Font.BOLD, 35));
        lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/image/Bank-1 (1).png")));

        JLabel lblNewLabel_1 = new JLabel("用户名：");
        lblNewLabel_1.setIcon(new ImageIcon(Login.class.getResource("/image/user.png")));
        lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 24));

        JLabel lblNewLabel_2 = new JLabel("密  码：");
        lblNewLabel_2.setIcon(new ImageIcon(Login.class.getResource("/image/Password.png")));
        lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 24));

        userNameText = new JTextField();
        userNameText.setFont(new Font("宋体", Font.PLAIN, 24));
        userNameText.setColumns(10);

        passwordText = new JPasswordField();
        passwordText.setFont(new Font("宋体", Font.PLAIN, 24));

        JButton btnNewButton = new JButton("登录");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                logininActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(Login.class.getResource("/image/login.png")));
        btnNewButton.setFont(new Font("宋体", Font.PLAIN, 20));

        JButton btnNewButton_1 = new JButton("重置");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetValueActionPerformed(e);
            }
        });
        btnNewButton_1.setIcon(new ImageIcon(Login.class.getResource("/image/reset.png")));
        btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 20));
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.TRAILING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(216)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                                        .addComponent(lblNewLabel_1)
                                                        .addComponent(lblNewLabel_2))
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(passwordText, GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                                                        .addComponent(userNameText, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE))
                                                .addGap(2))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
                                                .addGap(189)
                                                .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
                                                .addGap(31)))
                                .addGap(236))
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap(315, Short.MAX_VALUE)
                                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 357, GroupLayout.PREFERRED_SIZE)
                                .addGap(278))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(92)
                                .addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
                                .addGap(65)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(userNameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblNewLabel_1))
                                .addGap(110)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(passwordText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(96)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(btnNewButton))
                                .addContainerGap(125, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);

        //设置登录界面居中显示
        this.setLocationRelativeTo(null);
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 登录事件处理
     *
     * @param evt
     */
    private void logininActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String user_name = this.userNameText.getText();
        String user_password = new String(this.passwordText.getPassword());
        if (StringUtils.isEmpty(user_name)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空！");
            return;
        }
        if (StringUtils.isEmpty(user_password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return;
        }
        User user = new User(user_name, user_password);
        User currentUser = userDAO.login(user);
        if (currentUser != null) {
            JOptionPane.showMessageDialog(null, "登录成功！");
            dispose();
            new MainInterface().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "用户名或密码错误！");
        }
    }

    /**
     * 重置事件处理
     *
     * @param evt
     */
    private void resetValueActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        this.userNameText.setText("");
        this.passwordText.setText("");
    }
}
